import { _decorator, Component, Node, ProgressBar, Label } from 'cc';
import { Battle } from '../Battle';
import { EnEvent } from '../Common/const/EnEvent';
import { GameAPI } from '../Common/GameAPI';
const { ccclass, property } = _decorator;

export class GameProgressInfo {
    public level: number = 1;
    //每秒多少各怪
    public monsterNum: number = 20;
    public bossTime: number = 10;
}

@ccclass('GameProgressPanel')
export class GameProgressPanel extends Component {
    @property(ProgressBar)
    public progress: ProgressBar = null;
    @property(Label)
    private timeLabel: Label = null;

    private progressInfoData: Array<GameProgressInfo> = [];
    private totalTime: number = 0;
    private curTime: number = 0;
    private gameLevel: number = 0;
    private countDownTime: number = 0;
    private curProgressInfo: GameProgressInfo;

    private initBossProgress: boolean = false;

    private createTime: number = 0;

    start() {
        for (let index = 0; index < 3; index++) {
            let info: GameProgressInfo = new GameProgressInfo();
            info.level = index + 1;
            info.monsterNum = info.level * 10;
            info.bossTime = 15;
            this.totalTime += info.bossTime;
            this.progressInfoData.push(info);
        }
        GameAPI.RegGEvent(EnEvent.BeginBossProgressEvent, this.OnBeginBossProgressEvent.bind(this), this);
    }

    update() {
        if (Battle.startGame && !this.initBossProgress) {
            this.initBossProgress = true;
            GameAPI.FireGEvent(EnEvent.BeginBossProgressEvent);
        }
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    OnBeginBossProgressEvent() {
        if (this.gameLevel < this.progressInfoData.length) {
            this.createTime = 0;
            this.curProgressInfo = this.progressInfoData[this.gameLevel];
            this.countDownTime = this.curProgressInfo.bossTime;
            this.schedule(this.OnTime.bind(this), 1);
            this.gameLevel++;
        }
    }

    OnTime() {
        this.curTime++;
        this.countDownTime--;
        this.createTime++;
        if (this.countDownTime <= 0) {
            //要刷boss
            GameAPI.FireGEvent(EnEvent.BeginBossCreateEvent, this.gameLevel);
            this.unscheduleAllCallbacks();
        }
        this.progress.progress = this.curTime / this.totalTime;
        if (this.createTime <= 10) {
            if (this.gameLevel == 1) {
                Battle.createEnemyNum = 2;
            }
            else if (this.gameLevel == 2) {
                Battle.createEnemyNum = 3;
            }
            else if (this.gameLevel == 3) {
                Battle.createEnemyNum = 5;
            }
        }
        else {
            if (this.gameLevel == 1) {
                Battle.createEnemyNum = 3;
            }
            else if (this.gameLevel == 2) {
                Battle.createEnemyNum = 5;
            }
            else if (this.gameLevel == 3) {
                Battle.createEnemyNum =7;
            }
        }
    }


}


